/*
 *  Copyright 2010 mazhao.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package shopping.front.onthefly;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseOnTheFlyAction;
import shopping.commons.Util;
import shopping.model.ShoppingCart;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author mazhao
 */
@org.apache.struts2.convention.annotation.Namespace("/front/onthefly")
@org.apache.struts2.convention.annotation.ParentPackage(value="shopping-json-default")
@org.apache.struts2.convention.annotation.InterceptorRefs({
    @org.apache.struts2.convention.annotation.InterceptorRef("jsonValidationWorkflowStack"),
    @org.apache.struts2.convention.annotation.InterceptorRef("defaultStack")
})
public class PutCartAction extends BaseOnTheFlyAction {

    // properties from detail page
    private int productId = 0;

    // TODO: account -> count
    private int account = 0;

    // properties from cart page
    private List<String> cartItems = new ArrayList<String>();


    @Action(value="/front/onthefly/putcart",
        results={
            @Result(name="success", type="json", params = {"ignoreHierarchy", "false"})
        }
    )
    public String putCart() {
        
        if(log.isDebugEnabled()) {
            log.debug("enter /front/onthefly/putcart with product id " + productId + " and account " + account);
        }

        // get shopping cart, if no shopping cart then create it and put it to session.
        ShoppingCart sc = null;
        if(Util.Shopping.hasShoppingCart()) {
            sc = Util.Shopping.retriveShoppingCart();
        } else {
            sc = new ShoppingCart();
            Util.Shopping.saveShoppingCart(sc);
        }

        // put the product id and account to shopping cart
        sc.putItem(productId, account);
        
        // set status to tell client put successfully 
        this.onTheFlyStatus = true;
        this.onTheFlyMessage = "Put Cart Successfully!<br> For " + productId + " with count of " + sc.countItem(productId);
        
        return SUCCESS;
    }

    @Action(value="/front/onthefly/updatecart",
        results={
            @Result(name="success", type="json", params = {"ignoreHierarchy", "false"})
        }
    )
    public String updateCart() {

        if(log.isDebugEnabled()) {
            log.debug("enter /front/onthefly/updatecart " );
            log.debug(cartItems);
        }

        // remove old card
        ShoppingCart sc = null;
        if(Util.Shopping.hasShoppingCart()) {
            Util.Shopping.removeShoppingCart();
        }
        
        // create new shopping cart
        sc = new ShoppingCart();

        // add items to new shopping cart
        for(String s : cartItems) {
            String[] sa = s.split("_");
            Integer pid = new Integer(sa[0]);
            Integer acc   = new Integer(sa[1]);
            // if update to zero, then delete it.
            if(acc > 0) {
                sc.putItem(pid, acc);
            }
        }

        // save shopping cart to session
        Util.Shopping.saveShoppingCart(sc);

        // return successfully message.
        this.onTheFlyMessage = "Update Cart Successfully!";
        this.onTheFlyStatus = true;
        return SUCCESS;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public int getAccount() {
        return account;
    }

    public void setAccount(int account) {
        this.account = account;
    }

    public List<String> getCartItems() {
        return cartItems;
    }

    public void setCartItems(List<String> cartItems) {
        this.cartItems = cartItems;
    }

}
